package com.jzo2o.orders.manager.strategy.impl;

import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.mapper.OrdersCanceledMapper;
import com.jzo2o.orders.base.mapper.OrdersRefundMapper;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.base.model.domain.OrdersCanceled;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import com.jzo2o.orders.manager.service.impl.OrdersManagerServiceImpl;
import com.jzo2o.orders.manager.strategy.OrderCancelStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

//普通用户取消未支付订单
@Component("1:NO_PAY")//用户类型:订单状态
public class CommonUserNoPayOrderCancelStrategy implements OrderCancelStrategy {
    @Autowired
    private OrdersCanceledMapper ordersCanceledMapper;
    @Autowired
    private OrdersManagerServiceImpl ordersManagerService;


    /**
     * 订单取消
     *
     * @param orderCancelDTO 取消订单的参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancel(OrderCancelDTO orderCancelDTO) {
        //修改订单的状态为已取消，保存取消订单的记录
        //600 已取消
        ordersManagerService.lambdaUpdate().set(Orders::getOrdersStatus, OrderStatusEnum.CANCELED.getStatus())
                .eq(Orders::getPayStatus, OrderPayStatusEnum.NO_PAY.getStatus())
                .eq(Orders::getId, orderCancelDTO.getId()).update();
        // 2保存取消订单记录
        OrdersCanceled ordersCanceled = new OrdersCanceled();
        //订单id
        ordersCanceled.setId(orderCancelDTO.getId());
        //取消人ID
        ordersCanceled.setCancellerId(orderCancelDTO.getCurrentUserId());
        //取消人名称
        ordersCanceled.setCancelerName(orderCancelDTO.getCurrentUserName());
        //取消人类型，1：普通用户，4：运营人员
        ordersCanceled.setCancellerType(orderCancelDTO.getCurrentUserType());
        //取消原因
        ordersCanceled.setCancelReason(orderCancelDTO.getCancelReason());
        //取消时间
        ordersCanceled.setCancelTime(LocalDateTime.now());
        //订单取消映射器
        ordersCanceledMapper.insert(ordersCanceled);
    }
}
